package com.jinhua.huaoj.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinhua.huaoj.common.BaseResponse;
import com.jinhua.huaoj.common.ErrorCode;
import com.jinhua.huaoj.common.ResultUtils;
import com.jinhua.huaoj.exception.BusinessException;
import com.jinhua.huaoj.exception.ThrowUtils;
import com.jinhua.huaoj.judge.judge_service.JudgeService;
import com.jinhua.huaoj.model.dto.questionSubmit.QuestionSubmitAddRequest;
import com.jinhua.huaoj.model.dto.questionSubmit.QuestionSubmitQueryRequest;
import com.jinhua.huaoj.model.entity.QuestionSubmit;
import com.jinhua.huaoj.model.entity.User;
import com.jinhua.huaoj.model.vo.QuestionSubmitVO;
import com.jinhua.huaoj.service.QuestionSubmitService;
import com.jinhua.huaoj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.CompletableFuture;

/**
 * 题目提交接口（停止使用)
 */
@RestController
@RequestMapping("/question_submit")
@Slf4j
@Deprecated
public class QuestionSubmitController {

    @Resource
    private QuestionSubmitService questionSubmitService;

    @Resource
    private UserService userService;

    @Resource
    @Lazy
    private JudgeService judgeService;

    /**
     * @param questionSubmitAddRequest
     * @param request
     * @return id   提交id
     */
    @PostMapping("/")
    public BaseResponse<Long> submit(@RequestBody QuestionSubmitAddRequest questionSubmitAddRequest,
                                     HttpServletRequest request) {
        if (questionSubmitAddRequest == null || questionSubmitAddRequest.getQuestionId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // todo 检验参数

        // 登录才能提交
        final User loginUser = userService.getLoginUser(request);

        long questionSubmitId = questionSubmitService.doQuestionSubmit(questionSubmitAddRequest, loginUser);
        if (questionSubmitId <= 0) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        CompletableFuture.runAsync(() -> {
            judgeService.submit(questionSubmitId);
        });

        return ResultUtils.success(questionSubmitId);
    }

    /**
     * 分页获取当前用户创建的资源列表
     *
     * @param questionSubmitQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/my/list/page/vo")
    public BaseResponse<Page<QuestionSubmitVO>> listMyQuestionSubmitVOByPage(
            @RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest, HttpServletRequest request) {
        if (questionSubmitQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }


        long current = questionSubmitQueryRequest.getCurrent();
        long size = questionSubmitQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<QuestionSubmit> questionSubmitPage = questionSubmitService.page(new Page<>(current, size),
                questionSubmitService.getQueryWrapper(questionSubmitQueryRequest));
        if (questionSubmitPage == null || questionSubmitPage.getRecords() == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return ResultUtils.success(questionSubmitService.getQuestionSubmitVOPage(questionSubmitPage, request));
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<QuestionSubmitVO> getQuestionSubmitVOById(long id, HttpServletRequest request) {

        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        //查询提交记录 使用getVO类进行脱敏
        QuestionSubmit questionSubmit = questionSubmitService.getById(id);
        if (questionSubmit == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(questionSubmitService.getQuestionSubmitVO(questionSubmit, request));
    }

}
